package pl.mcnova.model.combo;

import java.util.ArrayList;
import java.util.List;

import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataListener;

import pl.mcnova.model.dao.DeviceDAO;

public class StringComboBoxModel implements ComboBoxModel {
	private List<String> data;
	private String selectedItem;
	private DeviceDAO deviceDAO;
	private String name;

	public StringComboBoxModel(String name) {
		this.name = name;
		deviceDAO = new DeviceDAO();
		data = deviceDAO.getAllStringsGroupedByNumbers(name);
		selectedItem = null;
	}

	public StringComboBoxModel(List<String> Devices) {

		if (Devices != null) {
			data = Devices;
			selectedItem = Devices.get(0);
		} else {
			data = new ArrayList<String>();
			selectedItem = null;
		}
	}

	@Override
	public int getSize() {
		if(data == null) return 0;
		return data.size();
	}

	@Override
	public Object getElementAt(int index) {
		if (index >= 0 && index < data.size()) {
			return data.get(index);
		}
		return null;
	}

	@Override
	public void addListDataListener(ListDataListener l) {
		// TODO Auto-generated method stub

	}

	@Override
	public void removeListDataListener(ListDataListener l) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setSelectedItem(Object anItem) {

		selectedItem = (String) anItem;
	}

	@Override
	public Object getSelectedItem() {
		return selectedItem;
	}

	public void refresh() {
		data = deviceDAO.getAllStringsGroupedByNumbers(name);
	}

}